var skill = new Vue({
    el: "#wap_skill",
    data() {
        return {
            services: services,
            proficiency:'',
            skillList:[],
            resumeId:'',
        }
    },
    created () {
        this.proficiency = this.init("get","/user/prop/skillProficiency/list?page=0&limit=10&name=");
        this.resumeId = $.cookie('resumeId');
        var storage=window.localStorage;
        this.skillList = JSON.parse(storage.skills);
        if(!this.skillList){
            this.skillList=[];
        }
        this.deleteSkill();
    },
    methods:{
        init:function (_type,_url) {
            var that=this;
            var obj = "";
            $.ajax({
                type: _type,
                async:false,
                url: that.services+_url,
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (res) {
                    var check = ApiLoginStatus(res);
                    if(check==false)
                        return false;
                    obj = (JSON.parse(res)).data;
                }
            });
            return obj;
        },
        addSkill:function() {
            var that = this;
            var obj = {
                skillName:'',
                proficiency:'',
                proficiencyName:'',
                id:'',
                name:'',
                resumeId:that.resumeId
            };
            that.skillList.push(obj);
            setTimeout(function() {
                useLayUI();
            },30);
        },
        saveSkill:function () {
            for (var i=0;i<this.skillList.length;i++){
                var skillName = this.skillList[i].skillName;
                var profic = this.skillList[i].proficiency;
                if(skillName==''||skillName==null){
                    layerMsg('技能名称不为空');
                    return
                }
                if(profic==''||profic==null){
                    layerMsg('请选择熟悉程度');
                    return
                }
            }
            var that = this;
            $.ajax({
                url: that.services + "/user/resume/professionalSkill/save"
                , type: "post"
                , contentType: "application/json;charset=utf-8"
                , xhrFields: {
                    withCredentials: true
                }
                , crossDomain: true
                , data: JSON.stringify({
                    "professionalSkill": that.skillList,
                })
                , success: function (res) {
                    if(res.success){
                        var check = ApiLoginStatus(res);
                        if(check==false)
                            return false;
                        layerMsg(res.message);
                        setTimeout(function () {
                            window.location.href="my-resume.html" ;
                        }, 500);
                    }
                }
            })
        },
        deleteSkill:function () {
            var that = this;
            $(document).on('click', '.delete-skill', function() {
                var this_skill = $(this).parent('li');

                var id =  $(this).data('id');
                var index =  $(this).data('index');
                if(id==''||id==null){
                    that.skillList.splice(index,1); 
                    // layerMsg('该技能信息尚未保存');
                    return
                }
                layer.confirm('删除后无法恢复，确认要删除吗', {title:false,closeBtn:0,scrollbar: false}, function(index){
                    $.ajax({
                        url: that.services + "/user/resume/professionalSkill/"+id
                        , type: "delete"
                        , contentType: "application/json;charset=utf-8"
                        , xhrFields: {
                            withCredentials: true
                        }
                        , crossDomain: true
                        , success: function (res) {
                            if(res.success){
                                var check = ApiLoginStatus(res);
                                if(check==false)
                                    return false;
                                layerMsg(res.message);
                                setTimeout(function () {
                                    window.location.href="my-resume.html" ;
                                }, 500);
                            }
                        }
                    })
                    layer.close(index);
                });
            })
        }
    }
});

layui.use('form', function () {
    form = layui.form;
    form.on('select(profic-list)', function (data) {
        var id = data.elem.getAttribute("data");
        skill.skillList[id].proficiency=data.value;
    });
});